/*
    This file is part of the substandard lisp implementation of Standard Lisp
    Copyright (C) 2011 Nicky Nickell

    substandard lisp is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
    builtins related to boolean operations as defined in section 3.10 
    of the The Standard Lisp Report
*/

function std_and(a, env) {
  var u = a[0];

  var last = null;
  for (var i = 0; i < u.length; i++) {
    last = evaluate(u[i], env);
    if (last == NIL) {
      return NIL;
    }
  }

  return last;
}
builtins['and'] = new builtin_function(std_and, 0, true, [CLASS_ANY], FTYPE_FEXPR);

function std_cond(a, env) {
  var u = a[0];

  for (var i = 0; i < u.length; i++) {
    var antecedent = u[i].car;
    var consequent = u[i].cdr.car;

    if (evaluate(antecedent, env) != NIL) {
      return evaluate(consequent, env);
    }
  }

  return NIL;
}
builtins['cond'] = new builtin_function(std_cond, 0, true, [TYPE_PAIR], FTYPE_FEXPR);

function std_not(a, env) {
  var u = a[0];

  return (u == NIL) ? T : NIL;
}
builtins['not'] = new builtin_function(std_not, 1, false, [CLASS_ANY], FTYPE_EXPR);

function std_or(a, env) {
  var u = a[0];

  var last = null;
  for (var i = 0; i < u.length; i++) {
    last = evaluate(u[i], env);
    if (last != NIL) {
      return last;
    }
  }
  return NIL;
}
builtins['or'] = new builtin_function(std_or, 0, true, [CLASS_ANY], FTYPE_FEXPR);
